<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: Engine</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">Engine</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/lib/engine_rb.html">
                lib/engine.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000071">add_update</a>&nbsp;&nbsp;
      <a href="#M000075">change_cash</a>&nbsp;&nbsp;
      <a href="#M000072">delete_trigger</a>&nbsp;&nbsp;
      <a href="#M000079">engine</a>&nbsp;&nbsp;
      <a href="#M000078">graph</a>&nbsp;&nbsp;
      <a href="#M000077">graph_today</a>&nbsp;&nbsp;
      <a href="#M000076">transfer</a>&nbsp;&nbsp;
      <a href="#M000074">update_ownership</a>&nbsp;&nbsp;
      <a href="#M000073">update_trigger</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Protected Instance methods</h3>

      <div id="method-M000071" class="method-detail">
        <a name="M000071"></a>

        <div class="method-heading">
          <a href="#M000071" class="method-signature">
          <span class="method-name">add_update</span><span class="method-args">(user_id, stock_id, quantity, what, price)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000071-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000071-source">
<pre>
   <span class="ruby-comment cmt"># File lib/engine.rb, line 3</span>
3:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_update</span>(<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">stock_id</span>, <span class="ruby-identifier">quantity</span>, <span class="ruby-identifier">what</span>, <span class="ruby-identifier">price</span>)
4:                 <span class="ruby-identifier">update</span> = <span class="ruby-constant">Update</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">:user_id</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">:stock_id</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">stock_id</span>, <span class="ruby-identifier">:quantity</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">quantity</span>, <span class="ruby-identifier">:what</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">what</span>, <span class="ruby-identifier">:price</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">price</span>)
5:                 <span class="ruby-identifier">update</span>.<span class="ruby-identifier">save!</span>
6:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000075" class="method-detail">
        <a name="M000075"></a>

        <div class="method-heading">
          <a href="#M000075" class="method-signature">
          <span class="method-name">change_cash</span><span class="method-args">(id, what, money)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000075-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000075-source">
<pre>
    <span class="ruby-comment cmt"># File lib/engine.rb, line 41</span>
41:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">change_cash</span>(<span class="ruby-identifier">id</span>, <span class="ruby-identifier">what</span>, <span class="ruby-identifier">money</span>)
42:                 <span class="ruby-identifier">user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">id</span>)
43:                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">what</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;+&quot;</span>
44:                         <span class="ruby-identifier">user</span>.<span class="ruby-identifier">cash</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">money</span>
45:                 <span class="ruby-keyword kw">else</span>
46:                         <span class="ruby-identifier">user</span>.<span class="ruby-identifier">cash</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">money</span>   
47:                 <span class="ruby-keyword kw">end</span>
48:                 <span class="ruby-identifier">user</span>.<span class="ruby-identifier">save!</span>
49:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000072" class="method-detail">
        <a name="M000072"></a>

        <div class="method-heading">
          <a href="#M000072" class="method-signature">
          <span class="method-name">delete_trigger</span><span class="method-args">(id)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000072-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000072-source">
<pre>
    <span class="ruby-comment cmt"># File lib/engine.rb, line 7</span>
 7:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">delete_trigger</span>(<span class="ruby-identifier">id</span>)
 8:                 <span class="ruby-identifier">trigger</span> = <span class="ruby-constant">Trigger</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">id</span>)            
 9:                 <span class="ruby-identifier">trigger</span>.<span class="ruby-identifier">destroy</span>
10:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000079" class="method-detail">
        <a name="M000079"></a>

        <div class="method-heading">
          <a href="#M000079" class="method-signature">
          <span class="method-name">engine</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000079-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000079-source">
<pre>
     <span class="ruby-comment cmt"># File lib/engine.rb, line 112</span>
112:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">engine</span>
113:                 <span class="ruby-identifier">stocks</span> = <span class="ruby-constant">Stock</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">:all</span>)
114:                 <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">stock</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">stocks</span>
115:                         <span class="ruby-identifier">triggers_buy</span> = <span class="ruby-constant">Trigger</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">stock</span>.<span class="ruby-identifier">id</span>, <span class="ruby-value str">&quot;buy&quot;</span>)
116:                         <span class="ruby-identifier">triggers_sell</span> = <span class="ruby-constant">Trigger</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">stock</span>.<span class="ruby-identifier">id</span>, <span class="ruby-value str">&quot;sell&quot;</span>)
117:                         <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">buys</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">triggers_buy</span>
118:                                 <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">sells</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">triggers_sell</span>
119:                                         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">user_id</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">user_id</span>
120:                                                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">price</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">price</span>
121:                                                         <span class="ruby-identifier">quantity</span> = <span class="ruby-identifier">transfer</span>(<span class="ruby-identifier">buys</span>, <span class="ruby-identifier">sells</span>)
122:                                                         <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">market_value</span> = <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">price</span>
123:                                                         <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">save!</span>
124:                                                         <span class="ruby-identifier">history</span> = <span class="ruby-constant">History</span>.<span class="ruby-identifier">top</span>(<span class="ruby-identifier">status</span>.<span class="ruby-identifier">stock_id</span>)
125:                                                         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">market_value</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">history</span>.<span class="ruby-identifier">high</span>
126:                                                                 <span class="ruby-identifier">history</span>.<span class="ruby-identifier">high</span> = <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">market_value</span>
127:                                                         <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">market_value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">history</span>.<span class="ruby-identifier">low</span>
128:                                                                 <span class="ruby-identifier">history</span>.<span class="ruby-identifier">low</span> = <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">market_value</span>
129:                                                         <span class="ruby-keyword kw">end</span>
130:                                                         <span class="ruby-identifier">history</span>.<span class="ruby-identifier">volume</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">quantity</span>
131:                                                         <span class="ruby-identifier">history</span>.<span class="ruby-identifier">save!</span>
132:                                                         <span class="ruby-identifier">graph_today</span>
133:                                                 <span class="ruby-keyword kw">else</span>
134:                                                         <span class="ruby-keyword kw">break</span>
135:                                                 <span class="ruby-keyword kw">end</span>
136:                                         <span class="ruby-keyword kw">end</span>
137:                                 <span class="ruby-keyword kw">end</span>
138:                         <span class="ruby-keyword kw">end</span>
139:                 <span class="ruby-keyword kw">end</span>   
140:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000078" class="method-detail">
        <a name="M000078"></a>

        <div class="method-heading">
          <a href="#M000078" class="method-signature">
          <span class="method-name">graph</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000078-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000078-source">
<pre>
     <span class="ruby-comment cmt"># File lib/engine.rb, line 90</span>
 90:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">graph</span>
 91:                 <span class="ruby-identifier">stocks</span> = <span class="ruby-constant">Stock</span>.<span class="ruby-identifier">open_stocks</span>
 92:                 <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">stock</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">stocks</span>
 93:                         <span class="ruby-identifier">graph</span> = <span class="ruby-constant">Scruffy</span><span class="ruby-operator">::</span><span class="ruby-constant">Graph</span>.<span class="ruby-identifier">new</span>
 94:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">title</span> = <span class="ruby-value str">&quot;Long Term Movement&quot;</span>
 95:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">renderer</span> = <span class="ruby-constant">Scruffy</span><span class="ruby-operator">::</span><span class="ruby-constant">Renderers</span><span class="ruby-operator">::</span><span class="ruby-constant">Standard</span>.<span class="ruby-identifier">new</span>
 96:                         <span class="ruby-identifier">histories</span> = <span class="ruby-constant">History</span>.<span class="ruby-identifier">find</span>(    <span class="ruby-identifier">:all</span>,
 97:                                                                                 <span class="ruby-identifier">:conditions</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-node">&quot;stock_id = '#{stock.id}'&quot;</span>)
 98:                         <span class="ruby-identifier">price</span> = <span class="ruby-identifier">marker</span> = <span class="ruby-identifier">volume</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
 99:                         <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">history</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">histories</span>
100:                                 <span class="ruby-identifier">price</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">history</span>.<span class="ruby-identifier">close</span>
101:                                 <span class="ruby-comment cmt">#marker &lt;&lt; history.date.to_formatted_s(:short)</span>
102:                                 <span class="ruby-identifier">volume</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">history</span>.<span class="ruby-identifier">volume</span>
103:                         <span class="ruby-keyword kw">end</span>
104:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">:line</span>, <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">name</span>, <span class="ruby-identifier">price</span>
105:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">:bar</span>,  <span class="ruby-value str">'Volume'</span>, <span class="ruby-identifier">volume</span>
106:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">point_markers</span> = <span class="ruby-identifier">marker</span>
107:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">render</span> <span class="ruby-identifier">:to</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;public/images/&quot;</span><span class="ruby-operator">+</span><span class="ruby-identifier">stock</span>.<span class="ruby-identifier">name</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;.svg&quot;</span>
108:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">render</span>  <span class="ruby-identifier">:width</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">300</span>, <span class="ruby-identifier">:height</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">200</span>,
109:                           <span class="ruby-identifier">:to</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;public/images/&quot;</span><span class="ruby-operator">+</span><span class="ruby-identifier">stock</span>.<span class="ruby-identifier">name</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;.png&quot;</span>, <span class="ruby-identifier">:as</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'png'</span>
110:                 <span class="ruby-keyword kw">end</span>   
111:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000077" class="method-detail">
        <a name="M000077"></a>

        <div class="method-heading">
          <a href="#M000077" class="method-signature">
          <span class="method-name">graph_today</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000077-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000077-source">
<pre>
    <span class="ruby-comment cmt"># File lib/engine.rb, line 76</span>
76:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">graph_today</span>
77:                 <span class="ruby-identifier">stocks</span> = <span class="ruby-constant">Stock</span>.<span class="ruby-identifier">open_stocks</span>
78:                 <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">stock</span> <span class="ruby-keyword kw">in</span> <span class="ruby-identifier">stocks</span>
79:                         <span class="ruby-identifier">graph</span> = <span class="ruby-constant">Scruffy</span><span class="ruby-operator">::</span><span class="ruby-constant">Graph</span>.<span class="ruby-identifier">new</span>
80:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">title</span> = <span class="ruby-value str">&quot;Today's Movement&quot;</span>
81:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">renderer</span> = <span class="ruby-constant">Scruffy</span><span class="ruby-operator">::</span><span class="ruby-constant">Renderers</span><span class="ruby-operator">::</span><span class="ruby-constant">Standard</span>.<span class="ruby-identifier">new</span>
82:                         <span class="ruby-identifier">history</span> = <span class="ruby-constant">History</span>.<span class="ruby-identifier">top</span>(<span class="ruby-identifier">stock</span>.<span class="ruby-identifier">id</span>)
83:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">:line</span>, <span class="ruby-identifier">stock</span>.<span class="ruby-identifier">name</span>, [<span class="ruby-identifier">history</span>.<span class="ruby-identifier">open</span>, <span class="ruby-identifier">history</span>.<span class="ruby-identifier">high</span>, <span class="ruby-identifier">history</span>.<span class="ruby-identifier">low</span>, <span class="ruby-identifier">history</span>.<span class="ruby-identifier">close</span>]
84:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">point_markers</span> = [<span class="ruby-value str">'Open'</span>, <span class="ruby-value str">'High'</span>, <span class="ruby-value str">'Low'</span>, <span class="ruby-value str">'Close'</span>]
85:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">render</span> <span class="ruby-identifier">:to</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;public/images/&quot;</span><span class="ruby-operator">+</span><span class="ruby-identifier">stock</span>.<span class="ruby-identifier">name</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;_today.svg&quot;</span>
86:                         <span class="ruby-identifier">graph</span>.<span class="ruby-identifier">render</span>  <span class="ruby-identifier">:width</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">300</span>, <span class="ruby-identifier">:height</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">200</span>,
87:                           <span class="ruby-identifier">:to</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;public/images/&quot;</span><span class="ruby-operator">+</span><span class="ruby-identifier">stock</span>.<span class="ruby-identifier">name</span><span class="ruby-operator">+</span><span class="ruby-value str">&quot;_today.png&quot;</span>, <span class="ruby-identifier">:as</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'png'</span>
88:                 <span class="ruby-keyword kw">end</span>
89:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000076" class="method-detail">
        <a name="M000076"></a>

        <div class="method-heading">
          <a href="#M000076" class="method-signature">
          <span class="method-name">transfer</span><span class="method-args">(buys, sells)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000076-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000076-source">
<pre>
    <span class="ruby-comment cmt"># File lib/engine.rb, line 51</span>
51:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">transfer</span>(<span class="ruby-identifier">buys</span>, <span class="ruby-identifier">sells</span>)
52:                 <span class="ruby-identifier">stock</span> = <span class="ruby-constant">Stock</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">stock_id</span>)
53:                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">quantity</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">quantity</span>
54:                         <span class="ruby-identifier">delete_trigger</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">id</span>)
55:                         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">quantity</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">quantity</span>
56:                                 <span class="ruby-identifier">delete_trigger</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">id</span>)
57:                         <span class="ruby-keyword kw">else</span>
58:                                 <span class="ruby-identifier">update_trigger</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">id</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">quantity</span>)
59:                         <span class="ruby-keyword kw">end</span>
60:                         <span class="ruby-identifier">update_ownership</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">stock_id</span>, <span class="ruby-value str">&quot;+&quot;</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">quantity</span>)
61:                         <span class="ruby-identifier">update_ownership</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">stock_id</span>, <span class="ruby-value str">&quot;-&quot;</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">quantity</span>)
62:                         <span class="ruby-identifier">quantity_traded</span> = <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">quantity</span>
63:                 <span class="ruby-keyword kw">else</span>
64:                         <span class="ruby-identifier">delete_trigger</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">id</span>)
65:                         <span class="ruby-identifier">update_trigger</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">id</span>, <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">quantity</span>)
66:                         <span class="ruby-identifier">update_ownership</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">stock_id</span>, <span class="ruby-value str">&quot;+&quot;</span>, <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">quantity</span>)
67:                         <span class="ruby-identifier">update_ownership</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">stock_id</span>, <span class="ruby-value str">&quot;-&quot;</span>, <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">quantity</span>)
68:                         <span class="ruby-identifier">quantity_traded</span> = <span class="ruby-identifier">sells</span>.<span class="ruby-identifier">quantity</span>
69:                 <span class="ruby-keyword kw">end</span>
70:                 <span class="ruby-identifier">change_cash</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-value str">&quot;-&quot;</span>, <span class="ruby-identifier">quantity_traded</span><span class="ruby-operator">*</span><span class="ruby-identifier">buys</span>.<span class="ruby-identifier">price</span>)
71:                 <span class="ruby-identifier">add_update</span>(<span class="ruby-identifier">buys</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">stock_id</span>, <span class="ruby-identifier">quantity_traded</span>, <span class="ruby-value str">&quot;bought&quot;</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">price</span>)
72:                 <span class="ruby-identifier">change_cash</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-value str">&quot;+&quot;</span>, <span class="ruby-identifier">quantity_traded</span><span class="ruby-operator">*</span><span class="ruby-identifier">buys</span>.<span class="ruby-identifier">price</span>)
73:                 <span class="ruby-identifier">add_update</span>(<span class="ruby-identifier">sells</span>.<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">stock_id</span>, <span class="ruby-identifier">quantity_traded</span>, <span class="ruby-value str">&quot;sold&quot;</span>, <span class="ruby-identifier">buys</span>.<span class="ruby-identifier">price</span>)
74:                 <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">quantity_traded</span>
75:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000074" class="method-detail">
        <a name="M000074"></a>

        <div class="method-heading">
          <a href="#M000074" class="method-signature">
          <span class="method-name">update_ownership</span><span class="method-args">(user_id, stock_id, what, quantity)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000074-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000074-source">
<pre>
    <span class="ruby-comment cmt"># File lib/engine.rb, line 18</span>
18:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_ownership</span>(<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">stock_id</span>, <span class="ruby-identifier">what</span>, <span class="ruby-identifier">quantity</span>)
19:                 <span class="ruby-identifier">shrink</span> = <span class="ruby-constant">Ownership</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">stock_id</span>)
20:                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">what</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;+&quot;</span>
21:                         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">nil?</span>
22:                                 <span class="ruby-identifier">shrink</span> = <span class="ruby-constant">Ownership</span>.<span class="ruby-identifier">new</span>
23:                                 <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">user_id</span> = <span class="ruby-identifier">user_id</span>
24:                                 <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">stock_id</span> = <span class="ruby-identifier">stock_id</span>
25:                                 <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">quantity</span> = <span class="ruby-identifier">quantity</span>
26:                         <span class="ruby-keyword kw">else</span>
27:                                 <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">quantity</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">quantity</span>
28:                         <span class="ruby-keyword kw">end</span>
29:                         <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">save</span>  
30:                 <span class="ruby-keyword kw">else</span>
31:                         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">quantity</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">quantity</span>
32:                                 <span class="ruby-identifier">ownership</span> = <span class="ruby-constant">Ownership</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">id</span>)               
33:                                 <span class="ruby-identifier">ownership</span>.<span class="ruby-identifier">destroy</span>
34:                         <span class="ruby-keyword kw">else</span>
35:                                 <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">quantity</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">quantity</span>
36:                                 <span class="ruby-identifier">shrink</span>.<span class="ruby-identifier">save</span>
37:                         <span class="ruby-keyword kw">end</span>
38:                 <span class="ruby-keyword kw">end</span>   
39:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000073" class="method-detail">
        <a name="M000073"></a>

        <div class="method-heading">
          <a href="#M000073" class="method-signature">
          <span class="method-name">update_trigger</span><span class="method-args">(id, quantity)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000073-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000073-source">
<pre>
    <span class="ruby-comment cmt"># File lib/engine.rb, line 12</span>
12:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_trigger</span>(<span class="ruby-identifier">id</span>, <span class="ruby-identifier">quantity</span>)
13:                 <span class="ruby-identifier">trigger</span> = <span class="ruby-constant">Trigger</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">id</span>)
14:                 <span class="ruby-identifier">trigger</span>.<span class="ruby-identifier">quantity</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">quantity</span>
15:                 <span class="ruby-identifier">trigger</span>.<span class="ruby-identifier">save</span>
16:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>